﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MetroFramework.Forms;

namespace sn_scan
{
    public partial class SNSetForm : MetroForm
    {
        public SNSetForm()
        {
            InitializeComponent();
        }

        private void buttonSNSave_Click(object sender, EventArgs e)
        {
            string lenth = textBoxSNLenth.Text;
            string startWith = textBoxSNStartWith.Text;
            string SNNum =  textBoxSNNum.Text;

            if(string .IsNullOrEmpty(lenth) || string.IsNullOrEmpty(SNNum))
            {
                MessageBox.Show("SN长度或SN数量不能为空");
                return;
            }

            int len = 0;
            int num = 0;
            try
            {
                len = Convert.ToInt32(lenth);
                num = Convert.ToInt32(SNNum);
            }
            catch
            {
                MessageBox.Show("长度和数量转换失败，请填写正确的数字");
                return;
            }

            GlobeData.num = num;
            GlobeData.len = len;
            GlobeData.startWith = startWith;

            string model = Utils.ReadConfig("model");

            if (string.IsNullOrEmpty(model))
            {
                MessageBox.Show("配置文件读错误，请重试");
                return;
            }

            GlobeData.configName = model;

            string iniPath = GlobeData.appPath + "\\conf\\" + model + "\\SN.ini";

            Utils.INIWrite("SN", "startText", startWith, iniPath);
            Utils.INIWrite("SN", "snLenth", lenth, iniPath);
            Utils.INIWrite("SN", "snNum", SNNum, iniPath);
        }

        private void SNSetForm_Load(object sender, EventArgs e)
        {
            textBoxSNLenth.Text = GlobeData.len.ToString();
            textBoxSNNum.Text = GlobeData.num.ToString();
            textBoxSNStartWith.Text = GlobeData.startWith;
        }
    }
}
